package com.universe.impl;

import java.awt.Graphics;

import javax.swing.JComponent;

import com.universe.objects.Body;
import com.universe.objects.Universe;
import com.universe.utils.Vector;

public class UniverseAdapter extends JComponent {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Universe universe;
	private int maxX;
	private int maxY;
	private double scaleFactorX;
	private double scaleFactorY;
	
	public UniverseAdapter(int maxX, int maxY){
		
		double BODY1_MASS = 1.5e30;
		Vector BODY1_POSITION = new Vector(0, 0.5e10);
		Vector BODY1_VELOCITY = new Vector(-7.0e4, 0);
		
		double BODY2_MASS = 1.5e30;
		Vector BODY2_POSITION = new Vector(0, -0.5e10);
		Vector BODY2_VELOCITY = new Vector(7.0e4, 0);
		
		double BODY3_MASS = 1.0e29;
		Vector BODY3_POSITION = new Vector(0, 4.0e10);
		Vector BODY3_VELOCITY = new Vector(-7.0e4, 0);
		
		double BODY4_MASS = 1.0e29;
		Vector BODY4_POSITION = new Vector(0, -4.0e10);
		Vector BODY4_VELOCITY = new Vector(7.0e4, 0);

		double UNI_SIZE = 1e11;

		universe = new Universe(10000, UNI_SIZE,
				new Body(BODY1_MASS, BODY1_POSITION, BODY1_VELOCITY), 
				new Body(BODY2_MASS, BODY2_POSITION, BODY2_VELOCITY), 
				new Body(BODY3_MASS, BODY3_POSITION, BODY3_VELOCITY),
				new Body(BODY4_MASS, BODY4_POSITION, BODY4_VELOCITY));
		
		this.maxX = maxX;
		this.maxY = maxY;
		this.scaleFactorX = maxX / universe.getSize();
		this.scaleFactorY = maxY / universe.getSize();
	}
	
	public void paint(Graphics g){
		for(Body b : universe.getBodies()){
			int x = (int)(maxX/2 + b.getPosition().get(0) * scaleFactorX);
			int y = (int)(maxY/2 - b.getPosition().get(1) * scaleFactorY);
			g.fillOval(x, y, 20, 20);
		}
	}
	
	public void step(){
		universe.iterate();
	}
	
}
